﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Petroleum.Tool;
using HZH_Controls.Controls;
using Petroleum.DB.Model;
using System.Text.RegularExpressions;
using Petroleum.Model;
using HZH_Controls.Forms;
using System.Data.SQLite;
using Newtonsoft.Json;
using PetroleumTool.BaseTool;

namespace Petroleum.UC
{

    public partial class Step13 : UserControl
    {
        List<Casing> casingList = Session.CasingProgram.CasingList;
        Casing casing;
        FormMain main = new FormMain();
        List<SlurryModel> slurries = new List<SlurryModel>();
        SlurryModel slurry;
        public Step13(FormMain main)
        {
            InitializeComponent();
            this.main = main;
            txtin.TextChanged += Txtin_TextChanged;
            txtinto.TextChanged += Txtinto_TextChanged;
            txtfujia.TextChanged += Txtfujia_TextChanged;
            txtzaojiang.TextChanged += Txtfujia_TextChanged;
            Load();
        }
        private void Txtfujia_TextChanged(object sender, EventArgs e)
        {
            changeData();
        }
        void Load()
        {
            List<KeyValuePair<string, string>> spudCom = casingList.Select(m =>
                new KeyValuePair<string, string>(m.SpudNo, m.SpudNo)
            ).ToList();
            cmdCasing.Source = spudCom;
            cmdCasing.SelectedIndex = 0;
            casing = casingList.FirstOrDefault(m => m.SpudNo == cmdCasing.SelectedText);
            cmdCasing.SelectedChangedEvent += cmdCasing_SelectedChangedEvent;
        }
        private void cmdCasing_SelectedChangedEvent(object sender, EventArgs e)
        {
            casing = casingList.FirstOrDefault(m => m.SpudNo == cmdCasing.SelectedText);
            InitData(casing);
        }
        void InitData(Casing casing)
        {
            if (casing != null)
            {
                lblbitsize.Text = casing.BitSize;
                lblCasingSize.Text = casing.CasingSize;
                txtinterval.InputText = casing.CasingIntervalFrom;
                txtintervalTO.InputText = casing.CasingIntervalTo;

                txtin.InputText = casing.CasingIntervalFrom;
                txtinto.InputText = casing.CasingIntervalTo;
                lblhuanrong.Text = ((double.Parse(lblbitsize.Text) * double.Parse(lblbitsize.Text) - double.Parse(lblCasingSize.Text) * double.Parse(lblCasingSize.Text)) * 3.1415926 / 4000).ToString("0.##");
            }
        }
        private void dgv_Load(object sender, EventArgs e)
        {
            List<DataGridViewColumnEntity> lstCulumns = new List<DataGridViewColumnEntity>();
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "SpudNo", HeadText = "SpudNo", Width = 150, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "BitSize", HeadText = "Bit Size (mm)", Width = 200, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "CasingSize", HeadText = "CasingSize", Width = 200, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "duan", HeadText = "段长", Width = 200, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "CementType", HeadText = "CementType", Width = 150, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "fujia", HeadText = "附加量", Width = 150, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "zaojiang", HeadText = "造浆量m3 / t", Width = 150, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "SlurryVolume", HeadText = "SlurryVolume", Width = 150, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "CementExcess", HeadText = "CementExcess", Width = 150, WidthType = SizeType.Absolute });
            this.dgv.Columns = lstCulumns;
        }
        private void SaveData()
        {
            if (casing != null)
            {
                if (slurry != null && !string.IsNullOrEmpty(slurry.SpudNo))
                {
                    slurries.Remove(slurry);
                }
                SlurryModel slurr = new SlurryModel();
                slurr.SpudNo = casing.SpudNo;
                slurr.BitSize = lblbitsize.Text;
                slurr.CasingSize = lblCasingSize.Text;
                slurr.duan = lblduanchang.Text;
                slurr.SlurryVolume = double.Parse(lblduanrongji.Text);
                slurr.CementType = txtcementtype.InputText;
                slurr.CementExcess = double.Parse(lblshuini.Text);
                slurr.fujia = txtfujia.InputText;
                slurr.zaojiang = txtzaojiang.InputText;
                slurries.Add(slurr);
                this.dgv.DataSource = slurries;
            }
        }
        private void Txtinto_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(txtin.InputText) && !string.IsNullOrWhiteSpace(txtinto.InputText))
            {
                lblduanchang.Text = (double.Parse(txtinto.InputText) - double.Parse(txtin.InputText)).ToString();
                changeData();
            }
        }
        private void Txtin_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(txtin.InputText) && !string.IsNullOrWhiteSpace(txtinto.InputText))
            {
                lblduanchang.Text = (double.Parse(txtinto.InputText) - double.Parse(txtin.InputText)).ToString();
                changeData();
            }
        }
        private void changeData()
        {
            if (!string.IsNullOrWhiteSpace(txtfujia.InputText) && !string.IsNullOrWhiteSpace(txtzaojiang.InputText))
            {
                lblduanrongji.Text = (((double.Parse(txtfujia.InputText) / 100) * double.Parse(lblhuanrong.Text) * double.Parse(lblduanchang.Text)) / 1000 + double.Parse(txtfujiazhi.InputText)).ToString("0.##");
                lblshuini.Text = DecimalHelper.CutDecimalStr((double.Parse(lblduanrongji.Text) / double.Parse(txtzaojiang.InputText) * 1.03),2);
            }
        }
        private void btnUp_BtnClick(object sender, EventArgs e)
        {
            main.AddControl(new Step12(main));
        }
        private void btnDown_BtnClick(object sender, EventArgs e)
        {
            Session.slurries = slurries;
            main.AddControl(new Step14(main));
        }
        private void btnadd_BtnClick(object sender, EventArgs e)
        {
            SaveData();
        }
        private void StripMenuDel_Click(object sender, EventArgs e)
        {
            if (dgv.SelectRow != null)
            {
                int row = dgv.SelectRow.RowIndex;
                if (row > -1)
                {
                    slurries.RemoveAt(row);
                    this.dgv.DataSource = slurries;
                    return;
                }
            }
            FrmTips.ShowTipsWarning(this.FindForm(), "请先选择要删除的行");
        }
        private void StripMenuEdit_Click(object sender, EventArgs e)
        {
            if (dgv.SelectRow != null)
            {
                int row = dgv.SelectRow.RowIndex;
                slurry = slurries[row];
                cmdCasing.SelectedValue = slurry.SpudNo;
                lblbitsize.Text = slurry.BitSize;
                lblCasingSize.Text = slurry.CasingSize;
                txtcementtype.InputText = slurry.CementType;
                txtzaojiang.InputText = slurry.zaojiang;
                txtfujia.InputText = slurry.fujia;
            }
        }
        private void btnword_BtnClick(object sender, EventArgs e)
        {
            var depthListOld = main.projects.Where(n => n.TableName == TableNames.slurries.ToString()).FirstOrDefault();
            if (depthListOld != null)
            {
                Session.slurries = JsonConvert.DeserializeObject<List<SlurryModel>>(depthListOld.TableData);
                dgv.DataSource = Session.slurries;
                slurries = Session.slurries;
            }
        }
        private void btnsavetodb_BtnClick(object sender, EventArgs e)
        {
            if (slurries.Count > 0)
            {
                Session.slurries = slurries;
                SQLiteDataReader reader = main.sqliteHelper.ReadFullTable("SessionData");
                var projects = EntityTranslate.Translate<SessionData>(reader);
                var slurrieslist = projects.Where(n => n.TableName == TableNames.slurries.ToString()).FirstOrDefault();
                if (slurrieslist != null)
                    SQLiteHelper.SaveData(main.sqliteHelper, TableNames.slurries.ToString(), JsonConvert.SerializeObject(Session.slurries));
                else
                    SQLiteHelper.SaveData(main.sqliteHelper, TableNames.slurries.ToString(), JsonConvert.SerializeObject(Session.slurries), 1);
                FrmTips.ShowTipsSuccess(new FormMain(), "数据保存成功！");
            }
        }
    }
}
